/*
 * Created by JFormDesigner on Sat Mar 22 09:15:45 CST 2025
 */

package com.xzp.bams.client;

import com.xzp.bams.entity.Account;
import com.xzp.bams.exception.QueryException;
import com.xzp.bams.exception.RegisterException;
import com.xzp.bams.exception.TypeException;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.SocketException;
import javax.swing.*;

/**
 * @author 许志鹏
 */
public class RgisterPanel extends JPanel {
    private ATMClient atm;

    public RgisterPanel(ATMClient atm) {
        this.atm = atm;
        initComponents();
    }

    private void submit(ActionEvent e) {
        //收集页面数据
        String name = nameTextField.getText();
        String personId = personIdTextField.getText();
        String mail = mailTextField.getText();
        String password = new String(passwordField.getPassword());
        String repassword = new String(repasswordField.getPassword());
        int type = comboBox.getSelectedIndex();

        //调用Bank银行业务
        /*try {
            Account account = atm.bank.register(password, repassword, name, personId, mail, type);
            //开户成功
            if(account!=null){
                //1.初始化BusinessPanel界面
                atm.account = account;
                atm.businessPanel.initBusiness();

                //2.跳转至BusinessPanel界面
                CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
                cardLayout.show(this.getParent() , "businessPanel");

                //3.清空注册界面数据
                clear();
            }
        } catch (RegisterException | TypeException | QueryException ex) {
            //开户失败
            //1.弹窗提醒失败信息
            JOptionPane.showMessageDialog(null , ex.getMessage());
            //2.清空密码相关的输入框
            passwordField.setText("");
            repasswordField.setText("");
        }*/

        //==================加入网络编程后  begin
        //1.发送指令   写PrintWriter
        atm.pw.println("register#"+password+"#"+repassword+"#"+name+"#"+personId+"#"+mail+"#"+type);
        atm.pw.flush();

        //4.等待服务器发送的结果  读ObjectInputStream
        try {
            Object o = atm.ois.readObject();

            if(o instanceof Account){
                //登录成功，跳转至业务界面，初始化业务界面中数据
                Account acc = (Account) o;
                if(acc!=null){
                    //JOptionPane.showMessageDialog(this , "登录成功");
                    //跳转至业务界面
                    CardLayout cardLayout = (CardLayout) this.getParent().getLayout();
                    cardLayout.show(this.getParent(),"businessPanel");
                    //初始化业务界面中数据
                    atm.account = acc;
                    atm.businessPanel.initBusiness();
                    //清空登录界面
                    clear();
                }
            }else if(o instanceof String){
                //登录失败，弹框异常信息，并清空密码框
                JOptionPane.showMessageDialog(this , o);
                passwordField.setText("");
            }
        }  catch (SocketException ex) {
            JOptionPane.showMessageDialog(this , "服务器已关闭");
            System.exit(0);
        }  catch (IOException ex) {
            throw new RuntimeException(ex);
        }  catch (ClassNotFoundException ex) {
            throw new RuntimeException(ex);
        }
        //==================加入网络编程后  end
    }




        private void back(ActionEvent e) {
            CardLayout cardLayout = (CardLayout)this.getParent().getLayout();
            //指定跳转至主界面
            cardLayout.show(this.getParent() , "mainPanel");
            clear();
        }


        public void clear(){
            nameTextField.setText("");
            personIdTextField.setText("");
            mailTextField.setText("");
            passwordField.setText("");
            repasswordField.setText("");
            addresTextFild.setText("");
        }

        private void register(ActionEvent e) {
            // TODO add your code here
        }












    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        button4 = new JButton();
        button5 = new JButton();
        button6 = new JButton();
        button7 = new JButton();
        comboBox = new JComboBox<>();
        nameTextField = new JTextField();
        personIdTextField = new JTextField();
        addresTextFild = new JTextField();
        mailTextField = new JTextField();
        passwordField = new JPasswordField();
        repasswordField = new JPasswordField();
        submitbutton8 = new JButton();
        backbutton8 = new JButton();

        //======== this ========
        setLayout(null);

        //---- label1 ----
        label1.setText("\u6ce8\u518c\u754c\u9762");
        add(label1);
        label1.setBounds(135, 30, 100, label1.getPreferredSize().height);

        //---- button1 ----
        button1.setText("\u8d26\u6237\u7c7b\u578b");
        add(button1);
        button1.setBounds(new Rectangle(new Point(60, 75), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u7528\u6237\u540d");
        add(button2);
        button2.setBounds(60, 115, 85, 25);

        //---- button3 ----
        button3.setText("\u5bc6\u7801");
        add(button3);
        button3.setBounds(60, 155, 75, 30);

        //---- button4 ----
        button4.setText("\u786e\u8ba4\u5bc6\u7801");
        add(button4);
        button4.setBounds(60, 195, 70, button4.getPreferredSize().height);

        //---- button5 ----
        button5.setText("\u8eab\u4efd\u8bc1\u53f7");
        add(button5);
        button5.setBounds(60, 245, 80, button5.getPreferredSize().height);

        //---- button6 ----
        button6.setText("\u8be6\u7ec6\u5730\u5740");
        add(button6);
        button6.setBounds(60, 290, 80, 30);

        //---- button7 ----
        button7.setText("E-Mail");
        add(button7);
        button7.setBounds(60, 325, 75, button7.getPreferredSize().height);

        //---- comboBox ----
        comboBox.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u50a8\u84c4\u8d26\u6237",
            "\u4fe1\u7528\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u7684\u50a8\u84c4\u8d26\u6237",
            "\u53ef\u8d37\u6b3e\u7684\u4fe1\u7528\u8d26\u6237"
        }));
        add(comboBox);
        comboBox.setBounds(170, 75, comboBox.getPreferredSize().width, 30);
        add(nameTextField);
        nameTextField.setBounds(175, 115, 95, 30);
        add(personIdTextField);
        personIdTextField.setBounds(175, 245, 95, 30);
        add(addresTextFild);
        addresTextFild.setBounds(180, 285, 95, 30);
        add(mailTextField);
        mailTextField.setBounds(180, 325, 85, mailTextField.getPreferredSize().height);
        add(passwordField);
        passwordField.setBounds(175, 160, 95, 30);
        add(repasswordField);
        repasswordField.setBounds(175, 200, 95, repasswordField.getPreferredSize().height);

        //---- submitbutton8 ----
        submitbutton8.setText("\u6ce8\u518c");
        submitbutton8.addActionListener(e -> {
			register(e);
			submit(e);
		});
        add(submitbutton8);
        submitbutton8.setBounds(new Rectangle(new Point(70, 400), submitbutton8.getPreferredSize()));

        //---- backbutton8 ----
        backbutton8.setText("\u8fd4\u56de");
        backbutton8.addActionListener(e -> back(e));
        add(backbutton8);
        backbutton8.setBounds(new Rectangle(new Point(195, 400), backbutton8.getPreferredSize()));

        setPreferredSize(new Dimension(400, 480));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    private void registerKeyboardAction() {
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JComboBox<String> comboBox;
    private JTextField nameTextField;
    private JTextField personIdTextField;
    private JTextField addresTextFild;
    private JTextField mailTextField;
    private JPasswordField passwordField;
    private JPasswordField repasswordField;
    private JButton submitbutton8;
    private JButton backbutton8;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
